<?php
namespace jy_commission;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	
	public function create_condition($conditions){
		if(!$conditions){
			return '';
		}
		if(!is_array($conditions)){
			$conditions=json_decode($conditions,true);
		}
		$level_up_types=$this->level_up_types;
		$current_level_up_types=$this->current_level_up_types();
		$str='';
		foreach($current_level_up_types as $key){
			$str.=$level_up_types[$key].'达'.$conditions[$key].';';
		}
		return $str;
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function otherfee_types(){
		$jy_renwushop = check_permission('jy_renwushop');
		$res=array(
			'0'=>'无',
		);
		if($jy_renwushop){
			$res['jy_renwushop']='任务商城分佣';
		}
		return $res;
	}
	public function get_area_code($area){
		if(!$area['province']){
			return '';
		}
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	public function upgoods($post){
		if(!$post['id'] && !$post['ignore'] && empty($post['commissions'])){
			return false;
		}
		$data=array(
			'goods_id'=>intval($post['goods_id']),
			'ignore'=>intval($post['ignore']),
			'level_id'=>intval($post['level_id']),
			'commissions'=>$post['commissions']?ijson_encode($post['commissions']):'',
		);
		$id=intval($post['id']);
		if($id){
			$this->update('goods',$data,array('id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$this->insert('goods',$data);
		}
		return true;
	}
	public function getgoodshtml($goods_id){
		$commission_goods=pdo_get('jy_commission_goods',array('goods_id'=>$goods_id,'uniacid'=>$_SESSION['uniacid']));
		$return['item']['jy_commission_id']=$commission_goods['id'];
		$return['item']['jy_commission']=$commission_goods['commissions']=$commission_goods['commissions']?json_decode($commission_goods['commissions'],true):array();
		$return['item']['jy_commission_ignore']=$commission_goods['ignore']?:0;
		$htmls=array();
		$htmls[]=array('title'=>'客户分销设置','type'=>'title');
		$htmls[]=array('name'=>'jy_commission_ignore','title'=>'是否关闭分销','type'=>'switch','help'=>'关闭后，该商品将不参与分销');
		$htmls[]=array('title'=>'[温馨提示]：默认等级适合所有等级（当不匹配等级的时候，都按默认等级的比例算！）','type'=>'tip');
		$commissions=array();
		$commissions[]=array('name'=>'level_id','title'=>'适用等级','type'=>'select','help'=>'','options'=>'jy_commissionlevels');
		$commissionset=plugin('jy_commission')->plugin_setting('setting');
		for($i=1;$i<=$commissionset['level'];$i++){
			$commissions[]=array('type'=>'string','name'=>'commission'.$i,'title'=>$i.'级佣金','help'=>'填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金');
		}
		if($commissionset['area_open']==1){
			$commissions[]=array('type'=>'switch','name'=>'area_open','title'=>'是否开启社区代理');
			$commissions[]=array('name'=>'area_rate','title'=>'社区代理佣金','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金');
			$commissions[]=array('name'=>'area_xunirate','title'=>'社区代理虚拟币','type'=>'string','help'=>'填写数字表示固定'.'，填写%，表示按百分比计算虚拟币');
		}
		$commissions[]=array('type'=>'string','name'=>'xuni_rate','title'=>'直推获得虚拟币','help'=>'填写数字表示实际额度，填写百分数表示比例。例如填写1表示发放1个虚拟币，填写1%表示发放商品价格1%的虚拟币');	
		$htmls[]=array('type'=>'array','show'=>array('jy_commission_ignore'=>0),'title'=>'佣金设置','name'=>'jy_commission','add_tip'=>'添加一个设置',
					'list'=>$commissions,
					'details'=>$commissions
		);
		$return['htmls']=$htmls;
		$page_setting=$this->plugin_setting('page_setting');
		$setting=$this->plugin_setting('setting');
		$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
		$return['levels']['0']=array('id'=>0,'title'=>$page_setting['demolevel']);
		return $return;
	}
	public function buy_creditfee($order,$member=array()){
		if(!$member['level_id']){
			$default_level=$this->plugin_setting('default_level');
			$feetype=$default_level['feetype'];
			$buy_creditfee=$default_level['buy_creditfee'];
			$buy_creditfrom=$default_level['buy_creditfrom'];
		}else{
			$item=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['commissions']=json_decode($item['commissions'],true);
			$feetype=$item['feetype'];
			$buy_creditfee=$item['commissions']['buy_creditfee'];
			$buy_creditfrom=$item['buy_creditfrom'];
		}
		$fee=$order['price']*$buy_creditfee/100;
		if(!$buy_creditfee || $fee<=0){
			return false;
		}
		$ispay=1;
		if($member['agent_id'] && $buy_creditfrom){
			$basic=model('core/member')->detail($member['agent_id'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
			if($basic['credit'.$feetype]>=$fee){
				model('core/member')->member_credit($member['agent_id'],'credit'.$feetype,$fee,false,'jy_commission','下级下单奖励给下级');	
			}else{
				$ispay=0;
			}
		}
		if($ispay){
			model('core/member')->member_credit($member['mid'],'credit'.$feetype,$fee,true,'jy_commission','下单奖励');
		}
		$feelog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$member['mid'],
				'agent_id'=>$member['agent_id'],
				'fee'=>$fee,
				'feetype'=>$feetype,
				'createtime'=>TIMESTAMP,
				'ispay'=>$ispay,
		);
		$this->insert('feelog',$feelog);
		return true;
	}
	public function getgroupaddfee($goods=array(),$mid=0,$agent_id=0,$isone=false){
		$mid=$mid?:$_SESSION['uid'];
		if(!$mid || !$goods['id']){
			return 0;
		}
		if(!$goods['pcate']){
			$goods['pcate']=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'pcate');
		}
		if(!$goods['pcate']){
			return 0;
		}
		$agent_id=$agent_id?:$this->getcolumn('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'agent_id');
		if(!$agent_id){
			return 0;
		}
		$level=$isone?1:12;
		$addfee=0;
		for($i=0;$i<$level;$i++){
			if(!$agent_id){
				break;
			}
			$groupset = pdo_getcolumn('jy_commission_shop',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id),'groupset');
			$agent_id=$this->getcolumn('member',array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']),'agent_id');
			$groupset=json_decode($groupset,true);
			if(!$groupset){
				continue;
			}
			$addfee +=$groupset[$goods['pcate']]?:0;
		}
		return $addfee;
	}
	public function upyuelog($month=''){
		if(!$this->setting['yuefee_open']){
			exi('每月消费达标分红未开启！','error');
		}
		$yuelog = pdo_get('jy_commission_yuelog',array('uniacid'=>$_SESSION['uniacid'],'type'=>0,'month'=>$month));
		if($yuelog['id']){
			exi($month.'已生成奖励！','error');
		}
		$level_ids=pdo_getall('jy_commission_level',array('ismonth'=>1,'uniacid'=>$_SESSION['uniacid']),'id','id');
		$level_ids=$level_ids?array_keys($level_ids):-1;
		$time=strtotime($month);
		$time+=100;
		$y=date('Y',$time);
		$m=date('m',$time);
		if($m=='12'){
			$y2=$y+1;
			$m2=1;
		}else{
			$y2=$y;
			$m2=$m+1;
		}
		$nowyue = mktime(0,0,0,$m2,1,$y2);
		$yesyue = mktime(0,0,0,$m,1,$y);
		$this->upyuefee1($yesyue,$nowyue);
		$members=pdo_getall('jy_commission_member',array('is_agent'=>1,'uniacid'=>$_SESSION['uniacid'],'level_id'=>$level_ids),array('mid'));
		$allred=0;
		$num=0;
		$yuefee_set=$this->setting['yuefee_set'];
		paixu($yuefee_set,'man','desc');
		foreach($members as $item){
			$order_price=pdo_getsum('jy_commission_order',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($yesyue,$nowyue)),'yuefee1');
			if($this->setting['yuefee_ji']){
				$jiorder_price=pdo_getsum('jy_commission_commission',array('type'=>20,'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($yesyue,$nowyue)),'yuefee1');
				$order_price +=$jiorder_price;
			}
			foreach($yuefee_set as $v){
				if($v['man']<=$order_price){
					$money=$order_price*$v['rate']/100;
					break;
				}
			}
			$isadd=pdo_get('jy_commission_commission',array('type'=>27,'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid'],'ordersn'=>$month));
			if($isadd['id']){
				if($isadd['status']<=1){
					pdo_update('jy_commission_commission',array('status'=>0,'createtime'=>$enddate,'order_price'=>$order_price,'money'=>$money),array('id'=>$isadd['id'],'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
				}
			}else{
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$item['mid'],
						'order_price'=>$order_price,
						'ordersn'=>$month,
						'money'=>$money,
						'createtime'=>$enddate,
						'status'=>0,
						'type'=>27,
				);
				$this->insert('commission',$new_commission);
			}
			if($money>0){
				$num++;
				$allred+=$money;
			}
		}
		$allfee=pdo_getsum('jy_commission_order',array('uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($yesyue,$nowyue)),'yuefee1');
		$feelog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'num'=>$num,
				'allfee'=>$allfee,
				'allred'=>$allred,
				'month'=>$month,
				'type'=>0,
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_commission_yuelog',$feelog);
		return true;
	}
	public function upyuelog2($month=''){
		if(!$this->setting['yuefee_open2']){
			exi('月度业绩达标分红未开启！','error');
		}
		$yuelog = pdo_get('jy_commission_yuelog',array('uniacid'=>$_SESSION['uniacid'],'type'=>2,'month'=>$month));
		if($yuelog['id']){
			exi($month.'已生成奖励！','error');
		}
		$time=strtotime($month);
		$time+=100;
		$y=date('Y',$time);
		$m=date('m',$time);
		if($m=='12'){
			$y2=$y+1;
			$m2=1;
		}else{
			$y2=$y;
			$m2=$m+1;
		}
		$enddate = mktime(0,0,0,$m2,1,$y2);
		$startdate = mktime(0,0,0,$m,1,$y);
		$this->upyuefee1($startdate,$enddate);
		$members=pdo_getall('jy_commission_member',array('is_agent'=>1,'uniacid'=>$_SESSION['uniacid']),array('mid','order_price'));
		$allred=0;
		$num=0;
		$yuefee_set2=$this->setting['yuefee_set2'];
		if(!count($yuefee_set2)){
			exi('未设置业绩阶梯！','error');
		}
		paixu($yuefee_set2,'man','desc');
		foreach($members as $item){
			$getmymoney=$this->getmymoney($item,$startdate,$enddate,$yuefee_set2,31);
			$mymoney=$getmymoney['mymoney'];
			$myorder_price=$getmymoney['myorder_price'];
			$isadd=pdo_get('jy_commission_commission',array('type'=>31,'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid'],'ordersn'=>$month));
			if($isadd['id']){
				if($isadd['status']<=1){
					pdo_update('jy_commission_commission',array('status'=>0,'createtime'=>$enddate,'order_price'=>$myorder_price,'money'=>$mymoney),array('id'=>$isadd['id'],'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
				}
			}else{
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$item['mid'],
						'order_price'=>$myorder_price,
						'ordersn'=>$month,
						'money'=>$mymoney,
						'remark'=>ijson_encode($getmymoney),
						'createtime'=>$enddate,
						'status'=>0,
						'type'=>31,
				);
				$this->insert('commission',$new_commission);
			}
			if($mymoney>0){
				$num++;
				$allred+=$mymoney;
			}
		}
		$allfee=pdo_getsum('jy_commission_order',array('uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yuefee2');
		$feelog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'num'=>$num,
				'allfee'=>$allfee,
				'allred'=>$allred,
				'month'=>$month,
				'type'=>2,
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_commission_yuelog',$feelog);
		return true;
	}
	public function upyearlog($month=''){
		if(!$this->setting['yearfee_open']){
			exi('年度业绩达标分红未开启！','error');
		}
		$yuelog = pdo_get('jy_commission_yuelog',array('uniacid'=>$_SESSION['uniacid'],'type'=>1,'month'=>$month));
		if($yuelog['id']){
			exi($month.'已生成奖励！','error');
		}
		$startdate=strtotime(date('Y-m-d',mktime(0, 0 , 0,1,1,date("Y")-1)));
		$enddate=strtotime(date('Y-m-d H:i:s',mktime(23,59,59,12,31,date("Y")-1)));
		$this->upyuefee1($startdate,$enddate);
		$members=pdo_getall('jy_commission_member',array('is_agent'=>1,'uniacid'=>$_SESSION['uniacid']),array('mid','order_price'));
		$allred=0;
		$num=0;
		$yearfee_set=$this->setting['yearfee_set'];
		paixu($yearfee_set,'man','desc');
		foreach($members as $item){
			$getmymoney=$this->getmymoney($item,$startdate,$enddate,$yearfee_set,30);
			$mymoney=$getmymoney['mymoney'];
			$myorder_price=$getmymoney['myorder_price'];
			$isadd=pdo_get('jy_commission_commission',array('type'=>30,'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid'],'ordersn'=>$month));
			if($isadd['id']){
				if($isadd['status']<=1){
					pdo_update('jy_commission_commission',array('status'=>0,'createtime'=>$enddate,'order_price'=>$myorder_price,'money'=>$mymoney),array('id'=>$isadd['id'],'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
				}
			}else{
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$item['mid'],
						'order_price'=>$myorder_price,
						'ordersn'=>$month,
						'money'=>$mymoney,
						'remark'=>ijson_encode($getmymoney),
						'createtime'=>$enddate,
						'status'=>0,
						'type'=>30,
				);
				$this->insert('commission',$new_commission);
			}
			if($mymoney>0){
				$num++;
				$allred+=$mymoney;
			}
		}
		$allfee=pdo_getsum('jy_commission_order',array('uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yearfee');
		$feelog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'num'=>$num,
				'allfee'=>$allfee,
				'allred'=>$allred,
				'month'=>$month,
				'type'=>1,
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_commission_yuelog',$feelog);
		return true;
	}
	public function getmymoney($item=array(),$startdate,$enddate,$yuefee_set=array(),$type=0){
		$yuefee_mybuy=$type==30?$this->setting['yearfee_mybuy']:$this->setting['yuefee_mybuy'];
		$nomy=$yuefee_mybuy?false:true;
		$getteamids=$this->model('member')->getteamids($item['mid'],12,$nomy);
		if(!$getteamids){
			$getteamids=-1;
		}
		if($type==30){
			$myorder_price=pdo_getsum('jy_commission_order',array('status <'=>4,'mid'=>$getteamids,'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yearfee');
		}elseif($type==31){
			$myorder_price=pdo_getsum('jy_commission_order',array('status <'=>4,'mid'=>$getteamids,'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yuefee1');
		}else{
			$myorder_price=pdo_getsum('jy_commission_order',array('status <'=>4,'mid'=>$getteamids,'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yuefee2');
		}
		$mymoney=0;
		foreach($yuefee_set as $v){
			if($v['man']<=$myorder_price){
				$mymoney=$myorder_price*$v['rate']/100;
				break;
			}
		}
		$out='';
		if($mymoney<=0){
			return array('mymoney'=>$mymoney,'outfee'=>0,'out'=>$out,'myorder_price'=>$myorder_price);
		}
		$mymembers=pdo_getall('jy_commission_member',array('agent_id'=>$item['mid'],'is_agent'=>1,'uniacid'=>$_SESSION['uniacid']),array('mid'));
		$outfee=0;
		foreach($mymembers as $myv){
			if($mymoney<=0){
				break;
			}
			$getteamids=$this->model('member')->getteamids($myv['mid'],12,$nomy);
			if(!$getteamids){
				continue;
				$getteamids=-1;
			}
			if($type==30){
				$order_price=pdo_getsum('jy_commission_order',array('status <'=>4,'mid'=>$getteamids,'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yearfee');
			}elseif($type==31){
				$order_price=pdo_getsum('jy_commission_order',array('status <'=>4,'mid'=>$getteamids,'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yuefee1');
			}else{
				$order_price=pdo_getsum('jy_commission_order',array('status <'=>4,'mid'=>$getteamids,'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yuefee2');
			}
			if($order_price<=0){
				continue;
			}
			foreach($yuefee_set as $v){
					if($v['man']<=$order_price){
						$fee=$order_price*$v['rate']/100;
						$mymoney -=$fee;
						$outfee +=$fee;
						$out .='id'.$myv['mid'].':-￥'.$fee.';';
						break;
					}
			}
		}
		return array('mymoney'=>$mymoney>0?$mymoney:0,'outfee'=>price($outfee,'int'),'out'=>$out,'myorder_price'=>$myorder_price>0?$myorder_price:0);
	}
	public function upmoney($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1,'isxuni'=>0);
		if($this->setting['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		$money=pdo_getsum('jy_commission_commission',$params,'money');
		if($money>0){
			pdo_update('jy_commission_member',array('money +='=>$money),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			$update=array(
				'status'=>3,
			);
			pdo_update('jy_commission_commission',$update,$params);
		}
		return '';
	}
	public function upyefee($type=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if($type==27 || $type==31){
			for($i=0;$i<12;$i++){
					if($i==0){
						$startdate = mktime(0,0,0,date('m'),1,date('Y'));
						$enddate = time();
					}else{
						$startdate = mktime(0,0,0,date('m')-$i,1,date('Y'));
						$i2=$i-1;
						if($i2){
							$enddate = mktime(0,0,0,date('m')-$i2,1,date('Y'));	
						}else{
							$enddate = mktime(0,0,0,date('m'),1,date('Y'));	
						}
					}
					$this->upyuefee1($startdate,$enddate);
					$this->uponeyefee($type,$startdate,$enddate,$mid);
			}
		}elseif($type==30){
			for($i=0;$i<2;$i++){
				if($i==0){
					$startdate=strtotime(date('Y-m-d H:i:s',mktime(23,59,59,12,31,date("Y")-1)));
					$enddate=time();
				}else{
					$startdate=strtotime(date('Y-m-d',mktime(0, 0 , 0,1,1,date("Y")-1)));
					$enddate=strtotime(date('Y-m-d H:i:s',mktime(23,59,59,12,31,date("Y")-1)));
				}
				$this->upyuefee1($startdate,$enddate);
				$this->uponeyefee($type,$startdate,$enddate,$mid);
			}
		}
		return '';
	}
	public function uponeyefee($type=0,$startdate=0,$enddate=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if($type==27){
			$time=$startdate+100;
			$month=date('Y-m',$time);
			$fee_set=$this->setting['yearfee_set']?:array();
		}elseif($type==31){
			$time=$startdate+100;
			$month=date('Y-m',$time);
			$fee_set=$this->setting['yuefee_set2']?:array();
		}elseif($type==30){
			$time=$startdate+100;
			$month=date('Y',$time);
			$fee_set=$this->setting['yearfee_set']?:array();
		}
		$isadd=pdo_get('jy_commission_commission',array('type'=>$type,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'ordersn'=>$month));
		if($isadd['id'] && $enddate<time()-1000 && $isadd['status']){
			return '';
		}
		paixu($fee_set,'man','desc');
		$item['mid']=$mid;
		if($type==27){
			$mymoney=0;
			$myorder_price=pdo_getsum('jy_commission_order',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yuefee1');
			if($this->setting['yuefee_ji']){
				$jiorder_price=pdo_getsum('jy_commission_commission',array('type'=>20,'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($startdate,$enddate)),'yuefee1');
				$myorder_price +=$jiorder_price;
			}
			foreach($fee_set as $v){
				if($v['man']<=$myorder_price){
					$mymoney=$myorder_price*$v['rate']/100;
					break;
				}
			}
		}elseif($type==30 || $type==31){
			$getmymoney=$this->getmymoney($item,$startdate,$enddate,$fee_set,$type);
			$mymoney=$getmymoney['mymoney'];
			$myorder_price=$getmymoney['myorder_price'];
		}
		if($isadd['id'] && !$isadd['status']){
			pdo_update('jy_commission_commission',array('remark'=>ijson_encode($getmymoney),'createtime'=>$enddate,'order_price'=>$myorder_price,'money'=>$mymoney),array('id'=>$isadd['id'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$new_commission=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'order_price'=>$myorder_price,
				'ordersn'=>$month,
				'money'=>$mymoney,
				'createtime'=>$enddate,
				'remark'=>ijson_encode($getmymoney),
				'status'=>0,
				'type'=>$type,
			);
			$this->insert('commission',$new_commission);
		}
		return '';
	}
	public function upyuefee1($yesyue=0,$nowyue=0,$mid=0,$id=0){
		$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
		if($yesyue>$yuestart-500){
			$params=array('yuefee1 >'=>0,'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($yesyue,$nowyue));
			if($mid){
				$params['mid']=$mid;
			}
			$isadd=pdo_get('jy_commission_order',$params);
		}
		if($isadd['id']){
			//return '';
		}
		if($nowyue<time()-1000){
			return '';
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($yesyue,$nowyue));
		if($mid){
			$params['mid']=$mid;
		}
		if($id){
			$params['id']=$id;
		}
		$list=pdo_getall('jy_commission_order',$params);
		foreach($list as $item){
			$orderid=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'id');
			$order_goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),array('goodsid','total_price'));
			$yuefee1=$item['order_price'];
			$yuefee2=$item['order_price'];
			$yearfee=$item['order_price'];
			foreach($order_goods as $goods){
				$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
				$total_price=$goods['total_price'];
				if($goods_commission['yuefee_close']){
					$yuefee1 -=$total_price;
				}
				if($goods_commission['yuefee_close2']){
					$yuefee2 -=$total_price;
				}
				if($goods_commission['yearfee_close']){
					$yearfee -=$total_price;
				}
			}
			$yuefee1=$yuefee1>0?$yuefee1:0;
			$yuefee2=$yuefee2>0?$yuefee2:0;
			$yearfee=$yearfee>0?$yearfee:0;
			pdo_update('jy_commission_order',array('yuefee1'=>$yuefee1,'yuefee2'=>$yuefee2,'yearfee'=>$yearfee),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			pdo_update('jy_commission_commission',array('yuefee1'=>$yuefee1,'yuefee2'=>$yuefee2,'yearfee'=>$yearfee),array('type'=>20,'mid'=>$item['mid'],'ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		}
		return '';
	}
}